package article

import (
	"strconv"
	"strings"
	"path"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/Core"
	"git.oschina.net/fuxiaohei/fuxiaohei-go.git/App/meta"
)

type Home struct {

}

func (this Home) Setup() {
	Core.SetRoute("GET:/articles", "[B]user:articles.html")
	Core.SetRoute("GET:/article/", "[B]user:article.html")
	Core.SetFunc("ArticleList", this.ArticleList)
	Core.SetFunc("ArticleSidebar", this.ArticleSidebar)
	Core.SetFunc("ArticleOne", this.ArticleOne)
}

// Show a list of articles
func (_ Home) ArticleList(b *Core.HxBehavior) string {
	size , _ := strconv.Atoi(meta.MetaString("article_size"))
	pageUrl := "/articles/"
	page := 1
	t := b.Template()
	condition := map[string]string{}
	if len(b.Request.Param) >= 4 {
		// page condition
		if b.Request.Param[2] == "page" {
			page, _ = strconv.Atoi(b.Request.Param[3])
			if page < 1 {
				page = 1
			}
		}
		// category condition
		if b.Request.Param[2] == "category" && len(b.Request.Param) >= 5 {
			category := categoryGetBySlug(b.Request.Param[3])
			if len(category) < 1 {
				b.Response.Redirect("/articles/")
				return ""
			}
			condition["category"] = category["id"]
			t.Assign("Category", category).Assign("IsCategoryBread", true)
			pageUrl = "/articles/category/" + category["slug"] + "/"
			// category page condition
			if len(b.Request.Param) >= 6 {
				page, _ = strconv.Atoi(b.Request.Param[5])
				if page < 1 {
					page = 1
				}
			}
		}
		// user condition
		if b.Request.Param[2] == "user" && len(b.Request.Param) >= 5 {
			user := userGetByName(b.Request.Param[3])
			if len(user) < 1 {
				b.Response.Redirect("/articles/")
				return ""
			}
			condition["author"] = user["id"]
			t.Assign("User", user).Assign("IsUserBread", true)
			pageUrl = "/articles/user/" + user["user"] + "/"
			// user page condition
			if len(b.Request.Param) >= 6 {
				page, _ = strconv.Atoi(b.Request.Param[5])
				if page < 1 {
					page = 1
				}
			}
		}
	}
	articles, pager := GetList(page, size, condition, pageUrl)
	t.Assign("Articles", articles).Assign("Pager", pager)
	t.Assign("ArticlesLength", len(articles))
	t.Js("js/marked.js")
	t.Share("TITLE", "文章")
	return t.Render("article/list.html")
}

// Show one article
func (_ Home) ArticleOne(b *Core.HxBehavior) string {
	// check slug
	if len(b.Request.Param) < 3 {
		b.Response.Redirect("/articles/")
		return ""
	}
	slug := b.Request.Param[2]
	slug = strings.Replace(slug, path.Ext(slug), "", -1)
	// get article by slug
	article := GetByCondition(map[string]string{"slug":slug})
	if len(article) < 1 {
		b.Response.Redirect("/articles/")
		return ""
	}
	// increase view count
	IncreaseView(article["id"])
	t := b.Template()
	t.Assign("Article", article)
	t.Js("js/marked.js")
	t.Share("TITLE", article["title"])
	return t.Render("article/one.html")
}

// Show article sidebar
func (_ Home) ArticleSidebar(b *Core.HxBehavior) string {
	t := b.Template()
	t.Assign("Categories", categoryGetList())
	t.Assign("PopArticles", PopList(4))
	return t.Render("article/sidebar.html")
}


